package com.sunther.document.entity;

import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.sunther.idb.common.util.WebResult;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @author csb
 * @description: TODO
 * @date 2023/5/6 9:45
 */
@TableName(value = "document_user_v1")
public class User extends BaseEntity{

    private static final long serialVersionUID = 1L;

    @TableId(value="id",type = IdType.AUTO)
    private Long id;

    private String name;

    private String telephone;

    private String projectId;

    @TableField(exist = false)
    private String projectText;

    private Integer roleId;

    @TableField(exist = false)
    private String roleText;

    private String nickName;

    private String password;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getProjectId() {
        return projectId;
    }

    public void setProjectId(String projectId) {
        this.projectId = projectId;
    }

    public Integer getRoleId() {
        return roleId;
    }

    public void setRoleId(Integer roleId) {
        this.roleId = roleId;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getProjectText() {
        return projectText;
    }

    public void setProjectText(String projectText) {
        this.projectText = projectText;
    }

    public String getRoleText() {
        return roleText;
    }

    public void setRoleText(String roleText) {
        this.roleText = roleText;
    }

    /**
     * 校验用户
     * @param user
     */
    public static WebResult checkUser(User user){

        Pattern p = null;
        Matcher m = null;
        boolean flg = true;

        //不符合检验格式的输入，接口返回：用户名格式错误，用户名支持英文、数字、下划线，且必须以字母开头，长度4-15位。
        p = Pattern.compile("[a-zA-Z]{1}[a-zA-Z0-9_]{3,14}");
        m = p.matcher(user.getName());
        flg = m.matches();
        if (!flg) {
            return WebResult.failure("用户名格式错误，用户名支持英文、数字、下划线，且必须以字母开头，长度4-15位");
        }

        if (null == user.getId()){
            //返回：密码格式错误，密码支持字母、数字，区分大小写，长度8-22位。
            p = Pattern.compile("[a-zA-Z0-9]{7,21}");
            m = p.matcher(user.getPassword());
            flg = m.matches();
            if (!flg) {
                return WebResult.failure("密码格式错误，密码支持字母、数字，区分大小写，长度8-22位。");
            }
        }
        return WebResult.success("通过");
    }
}
